//
//  AllPersonAndCarsViewController.m
//  LearningFMDB
//
//  Created by dihuijun on 17/9/13.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "AllPersonAndCarsViewController.h"
#import "Person.h"
#import "Car.h"
#import "SQLHelper.h"


#import "CarsHeaderFooterView.h"
#import "BodycarsCell.h"
@interface AllPersonAndCarsViewController ()<UITableViewDelegate,UITableViewDataSource>
/**   表格   */
@property (nonatomic, strong) UITableView *tableView;


/**   personArray   */
@property (nonatomic, strong) NSMutableArray*personArray;
/**   carArray   */
@property (nonatomic, strong) NSMutableArray*carArry;


@end
static NSString *IDHeader = @"IDHeader";
@implementation AllPersonAndCarsViewController
/**   personArray   */
- (NSMutableArray *)personArray{
    if(!_personArray){
        _personArray = [NSMutableArray array];
    }
    return _personArray;
}
/**   carArry   */
- (NSMutableArray *)carArry{
    if(!_carArry){
        _carArry = [NSMutableArray array];
    }
    return _carArry;
}



/**   表格   */
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //1.设置导航栏
    [self setupNavgation];
    //2.setupTableView
    [self setupTableView];
    //3.获取数据库数据
    [self getPersonsAndCars];
}

#pragma mark ---- 1.设置导航栏
- (void)setupNavgation{
    self.navigationItem.title = @"车库";
    self.view.backgroundColor = [UIColor whiteColor];
}
#pragma mark --- 2.setupTableView
- (void)setupTableView
{
    //1.add tableView
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[CarsHeaderFooterView class] forHeaderFooterViewReuseIdentifier:IDHeader];
}
#pragma mark --<UITableViewDelegate,UITableViewDataSource>

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 8;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    CarsHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:IDHeader];
    //数据
    headerView.data = self.personArray[section];
    //组
    headerView.section = section;
    //加载数据
    [headerView loadHeaderFooterData];
    
    
    return headerView;
//    HeaderCell.person = self.personArray[section];
//    UILabel *label =  [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
//    Person *person = self.personArray[section];
//    //      NSLog(@"name--%@",person.name);
//    label.text = [NSString stringWithFormat:@"%@ 的所有车",person.name];
//    label.font = [UIFont systemFontOfSize:13];
//    label.textAlignment = NSTextAlignmentCenter;
//    
//    
//    return label;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.personArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    NSMutableArray *array = self.carArry[section];
    
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cheKuCell";
    BodycarsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[BodycarsCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    NSMutableArray *carArray = self.carArry[indexPath.section];
    Car *car = carArray[indexPath.row];
    cell.data = car;
    [cell loadTableViewData];
    return cell;
}
/**
 * 滑动删除
 */
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //删除某一组的某一行
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        /*
         //方法一
        //拿到要删除的某一个人下面的车数据
        Person *p = self.personArray[indexPath.section];
        Car *car = self.carArry[indexPath.section][indexPath.row];
        //从数据库中删除某一个人下面的一辆车
        [[SQLHelper sharedSQLHelper]deleteCar:car fromPerson:p];
        //获取删除后的数据（删除后该人少了一辆车）,查询这个人下面还有几辆车
        NSMutableArray *pArray = [[SQLHelper sharedSQLHelper]getAllCarsFromPerson:p];
        //删除数组中这个人原有的所有车，从新添加新的车、
        [self.carArry[indexPath.section] removeAllObjects];
        [self.carArry[indexPath.section] addObjectsFromArray:pArray];
         //刷新表格
        [self.tableView reloadData];
        
        */
        //方法二
        NSMutableArray *carArr = self.carArry[indexPath.section];
        Car *car = carArr[indexPath.row];
        Person *person = self.personArray[indexPath.section];
        
        NSLog(@"car.id--%@,own_id--%@",car.car_id,car.own_id);
        //执行sql删除
        [[SQLHelper sharedSQLHelper]deleteCar:car fromPerson:person];
        //获取所有的人类
        self.personArray = [[SQLHelper sharedSQLHelper]getAllPerson];
        //初始化数据
        self.carArry = [NSMutableArray array];
        //重新根据人获取车信息
        for (Person *p  in self.personArray) {
            NSMutableArray *arry = [[SQLHelper sharedSQLHelper]getAllCarsFromPerson:p];
            [self.carArry addObject:arry];
        }
        //刷新表格
        [self.tableView reloadData];
        
    }
}


#pragma mark --- 3.获取数据库数据
- (void)getPersonsAndCars{
    //获取person数据
    self.personArray = [[SQLHelper sharedSQLHelper]getAllPerson];
    //获取person对应的车数据
    if (self.personArray.count) {
        
        for (Person *p in self.personArray) {
            NSMutableArray *carArray = [NSMutableArray array];
            carArray = [[SQLHelper sharedSQLHelper]getAllCarsFromPerson:p];
            [self.carArry addObject:carArray];
        }
    }
   // NSLog(@"%@ === \n\n%@",self.personArray,self.carArry);
    self.tableView.tableFooterView = [[UIView alloc] init];
}













@end
